package beeboxes;


/**
 * Created by zhangyingjie on 2022/10/18
 *
 * @function
 */
public class DecodeUtil {

    //解析。原始数据

    /**
     * 解析 pacs .成 设施码。卡号。等。根据不同的起始位、结束位。
     * @param pacs
     * @param cardStart
     * @param cardEnd
     * @return
     */
    public static String decodePacsToCardNo(String pacs,int cardStart,int cardEnd) {
        if (pacs == null || pacs.length()<1) return "";

        int startByte = cardStart / 8;
        int startBit = cardStart % 8 + 1;
        int endByte = cardEnd / 8;
        int endBit = cardEnd % 8 + 1;
        long cardNum = 0;
        boolean startFlag = false;
        byte[] cardByteArray = new byte[1024];

        if (pacs.length() % 2 == 1) {
            pacs = pacs + "0";
        }
        for (int i = 0; i < pacs.length() / 2; i++) {
            int index = i * 2;
            int val = Integer.parseInt(pacs.substring(index, index + 2), 16);
            cardByteArray[i] = (byte) val;
        }

        for (int i = startByte; i <= endByte; i++) {
            if (i == startByte && i == endByte) {
                for (int j = startBit; j <= endBit; j++) {
                    if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                        startFlag = true;
                    }
                    if (startFlag) {
                        cardNum = cardNum * 2;
                        if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                            cardNum += 1;
                        }
                    }
                }
            } else if (i == startByte && i != endByte) {
                for (int j = startBit; j <= 8; j++) {
                    if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                        startFlag = true;
                    }
                    if (startFlag) {
                        cardNum = cardNum * 2;
                        if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                            cardNum += 1;
                        }
                    }
                }
            } else if (i != startByte && i == endByte) {
                for (int j = 1; j <= endBit; j++) {
                    if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                        startFlag = true;
                    }
                    if (startFlag) {
                        cardNum = cardNum * 2;
                        if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                            cardNum += 1;
                        }
                    }
                }
            } else {
                for (int j = 1; j <= 8; j++) {
                    if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                        startFlag = true;
                    }
                    if (startFlag) {
                        cardNum = cardNum * 2;
                        if ((cardByteArray[i] & BIT_MASK(j, j)) != 0) {
                            cardNum += 1;
                        }
                    }
                }
            }
        }
        return Long.toHexString(cardNum);
    }

    private static int BIT_MASK(int a, int b) {
        int mask1 = (0xFF >> (a - 1));
        int mask2 = (0xFF << (8 - b));
        return mask1 & mask2;
    }
}
